import 'package:cashbook/model/Accounts.dart';
import 'package:cashbook/model/user_vo.dart';
import 'package:cashbook/utils/db_util/dbmanager.dart';
import 'package:cashbook/view/AddAccountsPage.dart';
import 'package:cashbook/view/ForgotPasswordPage.dart';
import 'package:cashbook/view/home.dart';
import 'package:cashbook/view/login.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/view/register.dart';
import 'package:cashbook/view/splash.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_) {
    runApp(MyApp());
  });
}

class MyApp extends StatelessWidget {
  Manager manager = new Manager(); //实例化一个全局变量
  @override
  Widget build(BuildContext context) {
    //initdb
    initDb();
    // TODO: implement build
    return MaterialApp(
      title: '记账本',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: <String, WidgetBuilder>{
        '/splash': (BuildContext context) => SplashPage(),
        '/home': (BuildContext context) => MyHomePage(),
        '/login': (BuildContext context) => LoginPage(),
        '/register': (BuildContext context) => RegisterPage(title: '注册'),
        '/forgot': (BuildContext context) => ForgotPasswordPage(title: '找回密码'),
        '/addaccount': (BuildContext context) => AddAccountsPage(title: '添加'),
      },
      //home: this.manager.isLogin ? MyHomePage() : SplashPage(),
      home: this.manager.isLogin ? MyHomePage() : SplashPage(),
    );
  }

  void initDb() {
    //1、初始化数据库
    DBManager.init();
    Future.delayed(Duration(seconds: 3), () {
      //2、创建表  用户表
      UserProvider().createTable();
      //3、创建账簿目录表
      AccountsProvider().createTable();
    });
  }
}
